package com.demo.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 
 * <p>
 * Title:ZipUtils.java
 * </p>
 * <p>
 * Description:基本的ZIP
 * </p>
 * 
 */
public class ZipUtils {

    private static final int BUFFER_SIZE = 1024;

    /**
     * 
     * <p>
     * Description:将文件压缩到zos中的指定目录下
     * </p>
     * 
     * @author bai
     * @date 2019年4月12日
     */
    public static void toZip(File srcFile, String dir, ZipOutputStream zos) throws IOException {
        if (srcFile.exists() && srcFile.isFile()) {
            ZipEntry zo = new ZipEntry(dir + "/" + srcFile.getName());
            zos.putNextEntry(zo);
            int len;
            byte[] buf = new byte[BUFFER_SIZE];
            try (FileInputStream in = new FileInputStream(srcFile)) {
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
            }
            zos.closeEntry();
        }
    }

}
